#!/bin/bash

echo "=== 车牌识别Release包闪退修复测试脚本 ==="

# 检查Flutter环境
echo "1. 检查Flutter环境..."
flutter --version

# 进入example目录
cd example

# 清理项目
echo "2. 清理项目..."
flutter clean
cd android
./gradlew clean
cd ..

# 获取依赖
echo "3. 获取依赖..."
flutter pub get

# 构建Release APK
echo "4. 构建Release APK..."
flutter build apk --release

# 检查APK是否生成成功
if [ -f "build/app/outputs/flutter-apk/app-release.apk" ]; then
    echo "✅ Release APK构建成功"
    echo "APK路径: build/app/outputs/flutter-apk/app-release.apk"
    
    # 获取APK大小
    apk_size=$(du -h build/app/outputs/flutter-apk/app-release.apk | cut -f1)
    echo "APK大小: $apk_size"
    
    # 尝试安装APK（如果有连接的设备）
    echo "5. 检查连接的设备..."
    adb devices
    
    device_count=$(adb devices | grep -c "device$")
    if [ $device_count -gt 0 ]; then
        echo "发现 $device_count 个设备，尝试安装APK..."
        adb install -r build/app/outputs/flutter-apk/app-release.apk
        
        if [ $? -eq 0 ]; then
            echo "✅ APK安装成功"
            echo "请在设备上测试车牌识别功能是否正常"
        else
            echo "❌ APK安装失败"
        fi
    else
        echo "⚠️  未发现连接的设备，请手动安装APK进行测试"
    fi
else
    echo "❌ Release APK构建失败"
    exit 1
fi

echo ""
echo "=== 测试完成 ==="
echo "请按以下步骤验证修复效果："
echo "1. 在设备上启动应用"
echo "2. 点击'启动车牌识别'按钮"
echo "3. 检查是否还会闪退"
echo "4. 测试相机预览和拍照功能"
echo ""
echo "如果仍有问题，请运行以下命令查看日志："
echo "adb logcat | grep -E '(PlateLprActivity|HyperLPR|AndroidRuntime)'"